package java6681.lesson14;
/*
*多态的好处以增加梯形为例：
* 增加梯形不需要再声明梯形对象，直接通过父类new梯形，用父类调用方法
* */
 abstract class 形状6681{
    public abstract void draw();
    public abstract double calcArea();
}
class 圆6681 extends 形状6681{
    private double r;

    public 圆6681() {
    }

    public 圆6681(double r) {
        this.r = r;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }

    @Override
    public void draw() {
        System.out.println("正在画圆...");
    }

    @Override
    public double calcArea() {
        double area = r*r*Math.PI;
        return area;
    }

    @Override
    public String toString() {
        return "圆："+"r="+getR();
    }
}
class 矩形6681 extends 形状6681{
    private double a;
    private double b;

    public 矩形6681() {
    }
    public 矩形6681(double a, double b) {
        this.a = a;
        this.b = b;
    }
    public double getA() {
        return a;
    }
    public void setA(double a) {
        this.a = a;
    }
    public double getB() {
        return b;
    }
    public void setB(double b) {
        this.b = b;
    }
    @Override
    public void draw() {
        System.out.println("正在画矩形...");
    }
    @Override
    public double calcArea() {
        double area = a*b;
        return area;
    }
    @Override
    public String toString() {
        return "矩形："+"a="+getA()+",b="+getB();
    }
}
class 三角形6681 extends 形状6681{
    private double h;
    private double bottom;

    public 三角形6681() {
    }

    public 三角形6681(double h, double bottom) {
        this.h = h;
        this.bottom = bottom;
    }

    public double getH() {
        return h;
    }

    public void setH(double h) {
        this.h = h;
    }

    public double getBottom() {
        return bottom;
    }

    public void setBottom(double bottom) {
        this.bottom = bottom;
    }
    @Override
    public void draw() {
        System.out.println("正在画三角形...");
    }

    @Override
    public double calcArea() {
        double area = (bottom*h)/2;
        return area;
    }
    @Override
    public String toString() {
        return "三角形："+"bottom="+getBottom()+",h="+getH();
    }
}
class 梯形6681 extends 形状6681{
    private double h;
    private double top;
    private double bottom;
    public 梯形6681() {
    }
    public 梯形6681(double h, double top, double bottom) {
        this.h = h;
        this.top = top;
        this.bottom = bottom;
    }
    public double getH() {
        return h;
    }
    public void setH(double h) {
        this.h = h;
    }
    public double getTop() {
        return top;
    }
    public void setTop(double top) {
        this.top = top;
    }
    public double getBottom() {
        return bottom;
    }
    public void setBottom(double bottom) {
        this.bottom = bottom;
    }
    @Override
    public void draw() {
        System.out.println("正在画梯形...");
    }
    @Override
    public double calcArea() {
        double area =( (top+bottom)*h)/2;
        return area;
    }
    @Override
    public String toString() {
        return "梯形："+"top="+getTop()+",bottom="+getBottom()+",h="+getH();
    }
}
class 计算器{
    public static void drawAll(形状6681[] shapes){
        for (int i = 0; i < shapes.length; i++) {
            System.out.println(shapes[i]+",area="+shapes[i].calcArea());
        }
    }
}
public class ShapeDemo6681 {
    public static void main(String[] args) {
        形状6681[] shapes = new 形状6681[]{new 圆6681(3.5),new 三角形6681(6.7,2.5),new 圆6681(5.0),
                new 梯形6681(3.5,8.0,7.0),new 矩形6681(2.0,3.0)};
        计算器.drawAll(shapes);


        /* 形状 sh = new 圆();
        sh.draw();
        sh = new 三角形();
        sh.draw();
        形状[] shapes = new 形状[]{new 圆(),new 三角形(),new 圆(),new 矩形(),new 梯形()};
        画图器.drawAll(shapes);*/
    }
}
